package com.yunsc.admin.controller.store;

import com.yunsc.common.utils.PageResult;
import com.yunsc.common.utils.Result;
import com.yunsc.common.utils.SecurityUtil;
import com.yunsc.common.validator.ValidationGroups;
import com.yunsc.store.dto.QueryStoreAddressDTO;
import com.yunsc.store.dto.StoreAddressDTO;
import com.yunsc.store.service.YunscStoreAddressService;
import com.yunsc.store.vo.StoreAddressVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-04-24 15:28
 */
@Api(value = "商家地址接口", tags = "商家信息接口")
@Slf4j
@RestController
@RequestMapping("/address")
public class AddressController {

    @Resource
    private YunscStoreAddressService storeAddressService;


    @ApiOperation(value = "查询商家地址", notes = "查询商家地址", httpMethod = "GET")
    @GetMapping("/list")
    public Result<?> queryStoreAddress(QueryStoreAddressDTO storeAddressDTO) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        PageResult<StoreAddressVO> addressList = storeAddressService.queryStoreAddressList(storeAddressDTO, currentStoreId);
        return Result.resultPageList(addressList);
    }

    @ApiOperation("添加商家地址")
    @PostMapping("/add")
    public Result<?> addStoreAddress(@RequestBody StoreAddressDTO addressDTO) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        storeAddressService.addStoreAddress(addressDTO, currentStoreId);
        return Result.ok();
    }


    @ApiOperation("编辑商家地址")
    @PostMapping("/edit")
    public Result<?> editStoreAddress(@RequestBody @Validated({ValidationGroups.Update.class}) StoreAddressDTO addressDTO) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        storeAddressService.editStoreAddress(addressDTO, currentStoreId);
        return Result.ok();
    }

    @ApiOperation("删除商家地址")
    @PostMapping("/delete")
    public Result<?> deleteStoreAddress(@RequestBody @Validated({ValidationGroups.Delete.class}) StoreAddressDTO addressDTO) {
         storeAddressService.deleteStoreAddress(addressDTO.getAddressId());
        return Result.ok();
    }


    @ApiOperation("查询商家退货地址列表")
    @GetMapping("/all")
    public Result<?> queryStoreAddressAll(@RequestParam("type") Integer type) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        List<StoreAddressVO> list = storeAddressService.queryStoreAddressAll(type, storeId);
        return Result.resultList(list);
    }


}
